package com.example.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.example.entity.Book;
import com.example.entity.Car;
import com.example.entity.Kind;
import com.example.entity.User;
import com.example.service.BookService;
import com.example.service.CarService;
import com.example.service.KindService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;
    @Autowired
    KindService kindService;
    @Autowired
    CarService carService;

    /**
     * 用户管理页面
     */
    @RequestMapping("/managerUser")
    public String managerUser(){
        return "managerUser";
    }

    /**
     * 获取用户数据(包括管理员)
     * @param user 查询条件包装实体类
     */
    @ResponseBody
    @RequestMapping(value="/getUserData", produces="application/json; charset=utf-8")
    public String getStudentData(@RequestBody User user){
        List<User> userList  = userService.findList(user);
        Integer listCount = userService.findAllCount(user);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", userList);
        map.put("count", listCount);
        return JSON.toJSONString(map);
    }

    /**
     * 删除用户
     * @param id  用户唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/deleteUser")
    public String deleteUser(String id){
        User user = userService.get(id);
        if(user.getUserType() == 0){
            //用户直接删除即可
            userService.delete(id);
            return "success";
        }else{
            //管理员判断是否只剩下一个
            Integer count = userService.selectCountByType(user.getUserType());
            if(count == 1){
                return "empty";
            }else{
                userService.delete(id);
                return "success";
            }

        }
    }

    /**
     * 用户表格页面
     */
    @RequestMapping(value="/userForm")
    public String userForm() {
        return "userForm";
    }

    /**
     * 根据id获取用户信息
     */
    @ResponseBody
    @RequestMapping(value="/getUserById")
    public String getUserById(String id){
        User u = userService.get(id);
        return JSON.toJSONString(u);
    }

    /**
     * 保存用户信息
     */
    @ResponseBody
    @RequestMapping(value="/saveUser")
    public String saveUser(@RequestBody User user){
        User u = userService.selectByNumber(user.getUserNumber(),user.getUserType());
        if(StrUtil.isEmpty(user.getId())){
            //新增
            if(u != null){
                return "numberRepeat";
            }
        }else{
            //修改
            if(u!= null && !u.getId().equals(user.getId())){
                return "numberRepeat";
            }
        }
        userService.save(user);
        return "success";
    }

    /**
     * 图书管理页面
     */
    @RequestMapping("/managerBook")
    public String managerBook(){
        return "managerBook";
    }

    /**
     * 获取书籍数据
     * @param book 查询条件包装实体类
     */
    @ResponseBody
    @RequestMapping(value="/getBookData", produces="application/json; charset=utf-8")
    public String getBookData(@RequestBody Book book){
        List<Book> bookList  = bookService.findList(book);
        Integer listCount = bookService.findAllCount(book);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", bookList);
        map.put("count", listCount);
        return JSON.toJSONString(map);
    }

    /**
     * 删除图书
     * @param id  图书唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/deleteBook")
    public String deleteBook(String id){
        //先删除购物车的内容
        List<Car> carList = carService.findList(new Car());
        carList = carList.stream().filter(o -> o.getBookId().equals(id)).collect(Collectors.toList());
        for(Car c : carList){
            carService.delete(c.getId());
        }
        //再删除图书
        Book b = bookService.get(id);
        File f = new File(FileController.filePrePath + b.getBookImg());
        f.delete();
        bookService.delete(id);
        return "success";
    }

    /**
     * 书籍表单页面
     */
    @RequestMapping(value="/bookForm")
    public String bookForm() {
        return "bookForm";
    }

    /**
     * 根据id获取书籍信息
     */
    @ResponseBody
    @RequestMapping(value="/getBookById")
    public String getBookById(String id){
        Book b = bookService.get(id);
        return JSON.toJSONString(b);
    }

    /**
     * 保存书籍信息
     */
    @ResponseBody
    @RequestMapping(value="/saveBook")
    public String saveBook(@RequestBody Book book){
        Book b = bookService.selectByNameAndPublisher(book.getBookName(), book.getBookPublisher());
        if(StrUtil.isEmpty(book.getId())){
            //新增
            if(b != null){
                return "bookRepeat";
            }
        }else{
            //修改
            if(b!= null && !b.getId().equals(book.getId())){
                return "bookRepeat";
            }
        }
        bookService.save(book);
        return "success";
    }

    /**
     * 图书类别管理页面
     */
    @RequestMapping("/managerKind")
    public String managerKind(){
        return "managerKind";
    }

    /**
     * 获取类别数据
     * @param kind 查询条件包装实体类
     */
    @ResponseBody
    @RequestMapping(value="/getKindData", produces="application/json; charset=utf-8")
    public String getKindData(@RequestBody Kind kind){
        List<Kind> kindList  = kindService.findList(kind);
        Integer listCount = kindService.findAllCount(kind);
        Map<String, Object> map = new HashMap<>(4);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", kindList);
        map.put("count", listCount);
        return JSON.toJSONString(map);
    }

    /**
     * 保存类别信息
     */
    @ResponseBody
    @RequestMapping(value="/saveKind")
    public String saveKind(String kindName){
        Kind kind = kindService.selectByName(kindName);
        if(kind != null){
            return "repeat";
        }else{
            Kind k = new Kind();
            k.setKindName(kindName);
            kindService.save(k);
            return "success";
        }
    }

    /**
     * 删除类别
     * @param id  类别唯一id
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/deleteKind")
    public String deleteKind(String id){
        //先把该类别的所有书籍类别字段设置为空
        Book b = new Book();
        b.setBookKind(id);
        List<Book> bookList = bookService.findList(b);
        for(Book book : bookList){
            book.setBookKind(null);
            bookService.save(book);
        }
        //最后再删除类别
        kindService.delete(id);
        return "success";
    }


}
